package nickthissen.iracing.parsing;

import android.content.Context;
import android.text.TextUtils;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.Date;

import nickthissen.iracing.datamodels.*;
import nickthissen.iracing.datamodels.Thread;

/**
 * Created by Nick on 22-5-13.
 */
public class LoginParser extends Parser
{
    public final static int LOGIN_CORRECT = 0;
    public final static int LOGIN_MAINTENACE = 1;
    public final static int LOGIN_INCORRECT_CREDENTIALS = 2;
    public final static int LOGIN_PITSTOP = 3;
    public final static int LOGIN_ERROR = 4;

    private final static String MESSAGE_URL = "http://members.iracing.com/updating/message.shtml?";

    public Context context;

    public class LoginResult extends Result
    {
        public int Status;
        public String Response;
    }

    @Override
    protected String getLogName() {
        return "LoginParser";
    }

    @Override
    protected Result createDefaultResultObject()
    {
        LoginResult result = new LoginResult();
        result.Message = "Error determining validity of credentials. Please check if the iRacing membersite is online.";
        return result;
    }

    @Override
    public Result doParse(String source)
    {
        LoginResult result = new LoginResult();

        result.Status = LOGIN_CORRECT;

        log("Start jsoup parsing login source");

        if (logLarge(context, source))
            log("Source logged.");
        else
            log("Source logging disabled.");

        Document doc = Jsoup.parse(source);

        // Check for maintenance
        if (doc.title().toLowerCase().contains("maintenance") &&
            doc.text().toLowerCase().contains("down for maintenance"))
        {
            result.Status = LOGIN_MAINTENACE;

            Elements frames = doc.body().select("iframe");
            if (frames.size() > 0)
            {
                Date date = new Date();
                String url = MESSAGE_URL + Long.toString(date.getTime());
                try
                {
                    Document messageDoc = Jsoup.connect(url).get();
                    result.Response = messageDoc.text();
                }
                catch (Exception ex)
                {
                    result.Response = "(error retrieving maintenance message)";
                }
            }
        }

        // Check for errors
        if (doc.title().toLowerCase().contains("error"))
        {
            result.Status = LOGIN_ERROR;
            result.Response = source;
            if (doc.text().contains("We Need a Pit Stop!"))
            {
                result.Status = LOGIN_PITSTOP;
            }
        }

        // Check for invalid credentials
        if (doc.title().startsWith("Authentication Failed") ||
                doc.text().contains("Invalid email address or password."))
        {
            result.Status = LOGIN_INCORRECT_CREDENTIALS;
        }

        result.Success = true;
        return result;
    }

}
